package com.mstore.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.mstore.action.ExchangeAction;
import com.mstore.dao.ExchangeGoodsDao;
import com.mstore.pojo.ExchangeGood;
import com.mstore.pojo.ExchangeLog;

public class ExchangeGoodsService {

	private ExchangeGoodsDao exchangeGoodsDao;
	private static Logger logger = Logger.getLogger(ExchangeAction.class
			.getName());

	// 添加兑换商品
	public void addExgood(ExchangeGood exchangeGood) {
		exchangeGoodsDao.addExgood(exchangeGood);
	}

	// 查询兑换商品
	public List<ExchangeGood> getAllExgoods(ExchangeGood exchangeGood) {
		List<ExchangeGood> exchangeGoods = new ArrayList<ExchangeGood>();
		List<Object> objects = exchangeGoodsDao.getAllExgoods(exchangeGood);
		for (Object object : objects) {
			ExchangeGood eg = (ExchangeGood) object;
			eg.setCreateTime(eg.getCreateTime().substring(0,
					eg.getCreateTime().indexOf(".")));
			exchangeGoods.add(eg);
		}
		return exchangeGoods;
	}

	/**
	 * 分页查询所有兑换商品
	 */
	public List<ExchangeGood> getAllExgoodsPage(ExchangeGood exchangeGood,
			int index) {
		List<ExchangeGood> exchangeGoods = new ArrayList<ExchangeGood>();
		List<Object> objects = exchangeGoodsDao.getAllExgoodsPage(exchangeGood,
				index);
		for (Object object : objects) {
			ExchangeGood eg = (ExchangeGood) object;
			eg.setCreateTime(eg.getCreateTime().substring(0,
					eg.getCreateTime().indexOf(".")));
			exchangeGoods.add(eg);
		}
		return exchangeGoods;
	}

	/**
	 * 查询总数
	 * 
	 * @param exchangeGood
	 * @return
	 */
	public int getAllExgoodsPageTotal(ExchangeGood exchangeGood) {
		List<Object> objects = exchangeGoodsDao.getAllExgoods(exchangeGood);
		return objects.size();
	}

	/**
	 * 通过企业id和shopid查询所有
	 * 
	 * @param exchangeGood
	 * @return
	 */
	public List<ExchangeGood> getAllExgoodList(ExchangeGood exchangeGood) {
		List<ExchangeGood> exchangeGoods = new ArrayList<ExchangeGood>();
		List<Object> objects = exchangeGoodsDao.getAllExgoodList(exchangeGood);
		for (Object object : objects) {
			ExchangeGood eg = (ExchangeGood) object;
			eg.setCreateTime(eg.getCreateTime().substring(0,
					eg.getCreateTime().indexOf(".")));
			exchangeGoods.add(eg);
		}
		return exchangeGoods;
	}

	/**
	 * 通过企业id和shopid分页查询
	 */
	public List<ExchangeLog> getAllExgoodListPage(ExchangeGood exchangeGood,
			int index) {
		List<ExchangeLog> exchangeLogs = new ArrayList<ExchangeLog>();
		List<Object> objects = exchangeGoodsDao.getAllExgoodListPage(
				exchangeGood, index);
		for (Object object : objects) {
			ExchangeLog el = (ExchangeLog) object;
			el.setCreateTime(el.getCreateTime().substring(0,
					el.getCreateTime().indexOf(".")));
			exchangeLogs.add(el);
		}
		return exchangeLogs;
	}

	/**
	 * 查询总数
	 * 
	 * @param exchangeLog
	 * @return
	 */
	public int getAllExgoodListPageTotal(ExchangeGood exchangeGood) {
		List<Object> objects = exchangeGoodsDao
				.getAllExgoodListPageTotal(exchangeGood);
		return objects.size();
	}

	public Object getOneExgoodbyId(Long id) {
		Object object = exchangeGoodsDao.getOneExgoodbyId(id);
		return object;
	}

	public void updateExgoodamout(ExchangeGood exchangeGood) {
		exchangeGoodsDao.updateExgoodamout(exchangeGood);
	}

	// 通过店铺id查询兑换商品信息
	public List<ExchangeGood> getAllExgoodsByShopId(ExchangeGood exchangeGood) {
		List<ExchangeGood> exchangeGoods = new ArrayList<ExchangeGood>();
		List<Object> objects = exchangeGoodsDao
				.getAllExgoodsByShopId(exchangeGood);
		for (Object object : objects) {
			ExchangeGood eg = (ExchangeGood) object;
			eg.setCreateTime(eg.getCreateTime().substring(0,
					eg.getCreateTime().indexOf(".")));
			exchangeGoods.add(eg);
		}
		return exchangeGoods;
	}

	// 验证商品名称是否重复
	public int verifyExgoodsName(ExchangeGood exchangeGood) {
		List<Object> objects = exchangeGoodsDao
				.getExgoodsByGoodnameAndShopId(exchangeGood);
		int count = objects.size();
		return count;
	}

	// 修改时验证商品名称是否重复
	public int verifyGoodnameById(ExchangeGood exchangeGood) {
		List<Object> objects = exchangeGoodsDao
				.getExgoodsByGoodnameAndId(exchangeGood);
		int count = objects.size();
		return count;
	}

	// 删除商品
	public void delExgood(ExchangeGood exchangeGood) {
		exchangeGoodsDao.delExgood(exchangeGood);
	}

	// 通过id查询兑换商品信息
	public ExchangeGood getExgoodById(ExchangeGood exchangeGood) {
		List<Object> objects = exchangeGoodsDao.getExgoodById(exchangeGood);
		if (objects.size() > 0) {
			return (ExchangeGood) objects.get(0);
		}
		return null;
	}

	// 修改兑换商品信息
	public void updateExgood(ExchangeGood exchangeGood) {
		exchangeGoodsDao.updateExgood(exchangeGood);
	}

	// 查询兑换记录
	public List<ExchangeLog> getAllExLog(ExchangeLog exchangeLog) {
		List<ExchangeLog> exchangeLogs = new ArrayList<ExchangeLog>();
		List<Object> objects = exchangeGoodsDao.getAllExLog(exchangeLog);
		for (Object object : objects) {
			ExchangeLog el = (ExchangeLog) object;
			el.setCreateTime(el.getCreateTime().substring(0,
					el.getCreateTime().indexOf(".")));
			exchangeLogs.add(el);
		}
		return exchangeLogs;
	}

	// 分页查询兑换记录
	public List<ExchangeLog> getAllExLogPage(int index, ExchangeLog exchangeLog) {
		List<ExchangeLog> exchangeLogs = new ArrayList<ExchangeLog>();
		List<Object> objects = exchangeGoodsDao.getAllExLogPage(index,
				exchangeLog);
		for (Object object : objects) {
			ExchangeLog el = (ExchangeLog) object;
			el.setCreateTime(el.getCreateTime().substring(0,
					el.getCreateTime().indexOf(".")));
			exchangeLogs.add(el);
		}
		return exchangeLogs;
	}

	// 查询兑换记录条数
	public int getExLogCount(ExchangeLog exchangeLog) {
		return exchangeGoodsDao.getExLogCount(exchangeLog);
	}

	// 通过会员id查询兑换记录的条数
	public int getExLogCountByMemberId(ExchangeLog exchangeLog) {
		return exchangeGoodsDao.getExLogCountByMemberId(exchangeLog);
	}

	// 通过会员id分页查询兑换记录
	public List<ExchangeLog> getExLogPageByMemberId(int index,
			ExchangeLog exchangeLog) {
		List<ExchangeLog> exchangeLogs = new ArrayList<ExchangeLog>();
		List<Object> objects = exchangeGoodsDao.getExLogPageByMemberId(index,
				exchangeLog);
		for (Object object : objects) {
			ExchangeLog el = (ExchangeLog) object;
			el.setCreateTime(el.getCreateTime().substring(0,
					el.getCreateTime().indexOf(".")));
			exchangeLogs.add(el);
		}
		return exchangeLogs;
	}

	/**
	 * 通过企业id和商品id
	 */
	public List<ExchangeGood> getExgoodsPage(ExchangeGood exchangeGood,
			int index) {
		List<ExchangeGood> exchangeGoods = new ArrayList<ExchangeGood>();
		List<Object> objects = exchangeGoodsDao.getExgoodsPage(exchangeGood,
				index);
		for (Object object : objects) {
			ExchangeGood el = (ExchangeGood) object;
			el.setCreateTime(el.getCreateTime().substring(0,
					el.getCreateTime().indexOf(".")));
			exchangeGoods.add(el);
		}
		return exchangeGoods;

	}

	/**
	 * 通过企业id和商品id总页数
	 */
	public int getExgoodsPageTotal(ExchangeGood exchangeGood) {
		List<Object> objects = exchangeGoodsDao
				.getExgoodsPageTotal(exchangeGood);
		return objects.size();

	}

	/**
	 * 查询所有进行去重
	 * 
	 * @param exchangeGood
	 * @return
	 */
	public List<ExchangeGood> getAllExgoodsDistinct(ExchangeGood exchangeGood) {
		List<Object> objects = exchangeGoodsDao
				.getAllExgoodsDistinct(exchangeGood);
		List<ExchangeGood> exchangeLogs = new ArrayList<ExchangeGood>();
		for (int i = 0; i < objects.size(); i++) {
			ExchangeGood e = (ExchangeGood) objects.get(i);
			exchangeLogs.add(e);
		}
		return exchangeLogs;
	}

	public List<ExchangeGood> getExgoodsByGoodname(ExchangeGood exchangeGood) {
		List<Object> objects = exchangeGoodsDao
				.getExgoodsByGoodname(exchangeGood);
		List<ExchangeGood> exchangeLogs = new ArrayList<ExchangeGood>();
		for (int i = 0; i < objects.size(); i++) {
			ExchangeGood e = (ExchangeGood) objects.get(i);
			exchangeLogs.add(e);
		}
		return exchangeLogs;
	}

	public List<ExchangeLog> getExgoodsByGoodidPage(ExchangeLog exchangeLog,
			int index) {
		List<ExchangeLog> exchangeLogs = new ArrayList<ExchangeLog>();
		logger.info("exchangeLogService=" + exchangeLog);
		List<Object> objects = exchangeGoodsDao.getExgoodsByGoodidPage(
				exchangeLog, index);
		for (Object object : objects) {
			ExchangeLog el = (ExchangeLog) object;
			el.setCreateTime(el.getCreateTime().substring(0,
					el.getCreateTime().indexOf(".")));
			exchangeLogs.add(el);
		}
		logger.info("exchangeService=" + exchangeLogs);
		return exchangeLogs;
	}

	/**
	 * 通过企业id和商品id总页数
	 */
	public List<ExchangeGood> getExgoodsPageTotal1(ExchangeGood exchangeGood) {
		List<ExchangeGood> goods = new ArrayList<ExchangeGood>();
		List<Object> objects = exchangeGoodsDao
				.getExgoodsPageTotal1(exchangeGood);
		for (int i = 0; i < objects.size(); i++) {
			ExchangeGood e = (ExchangeGood) objects.get(i);
			goods.add(e);
		}
		return goods;
	}
	
	/**
	 * 通过条件计算兑换商品总数量
	 */
	public int getCountExgGoodsByCondition(ExchangeGood exchangeGood) {
		return exchangeGoodsDao.getCountExgGoodsByCondition(exchangeGood);
	}
	
	/**
	 * 通过条件查询兑换商品
	 * 分页
	 */
	public List<ExchangeGood> getPageExgGoodsByCondition(int index,
			ExchangeGood exchangeGood) {
		List<ExchangeGood> goods = new ArrayList<ExchangeGood>();
		List<Object> list = exchangeGoodsDao.getPageExgGoodsByCondition(index, exchangeGood);
		for (Object object : list) {
			goods.add((ExchangeGood)object);
		}
		
		return goods;
	}

	public int getExgoodsByGoodidPageTotal(ExchangeLog exchangeLog) {
		return exchangeGoodsDao.getExgoodsByGoodidPageTotal(exchangeLog).size();
	}

	public void setExchangeGoodsDao(ExchangeGoodsDao exchangeGoodsDao) {
		this.exchangeGoodsDao = exchangeGoodsDao;
	}
}
